<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <!--    <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css"  media="all">-->
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="//unpkg.com/layui@2.6.8/dist/css/layui.css"/>
    <!-- 引入 layui.js -->
</head>

<body>

<div class="layui-form-item" name="last[]" id="main" tag="0">
    <div class="layui-input-block" style="margin-left: 480px">
        <button id="login" type="button" class="layui-btn layui-btn-normal layui-btn-sm">
            <i class="layui-icon">登录账号</i>
        </button>
        <button id="add" type="button" class="layui-btn layui-btn-warm layui-btn-sm">
            <i class="layui-icon">添加账号</i>
        </button>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">主号</label>
        <div class="layui-input-inline">
            <input type="text" name="mainNumber[]" id="mainNumber_0" required lay-verify="required" placeholder="请输入账号"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">每单注数</label>
            <div class="layui-input-inline">
                <input type="text" name="note[]" id="note_0" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">每注金额</label>
            <div class="layui-input-inline">
                <input type="text" name="money[]" id="money_0" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">止盈设置</label>
            <div class="layui-input-inline">
                <input type="text" name="take[]" id="take_0" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
            <label class="layui-form-label">下单间隔</label>
            <div class="layui-input-inline">
                <input type="text" name="interval_one[]" id="interval_one_0" placeholder="请输入" autocomplete="off"class="layui-input">
            </div>
            <div class="layui-input-inline">
                <input type="text" name="interval_one[]" id="interval_two_0" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-inline" id="difference_0">

        </div>
        <div class="layui-input-inline" id="interval_one_load_0">

        </div>
        <div class="layui-input-inline" id="Interval_two_load_0">

        </div>
        <div class="layui-input-inline" id="balance_0">

        </div>
        <div class="layui-input-inline" id="value_0">

        </div>
    </div>

<!--    <div name="orderNum[]" >-->

<!--        <textarea rows="10" cols="50" id="orderNums_0"  style="margin-left: 10px;">-->
<!--        当前单号：0-->
<!--        </textarea>-->
<!--    </div>-->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label"></label>
            <button name="subOrder[]" type="button" onclick="subOrder(0)"
                    class="layui-btn layui-btn-normal layui-btn-sm">
                <i class="layui-icon">开始下单</i>
            </button>
            <button name="stopOrder[]" type="button" onclick="stopOrder(0)"
                    class="layui-btn layui-btn-normal layui-btn-sm">
                <i class="layui-icon">停止下单</i>
            </button>
        </div>
    </div>

</div>


</body>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['element', 'jquery', 'layer', 'form', 'laydate'], function () {
        var form = layui.form
            , element = layui.element
            , laydate = layui.laydate
            , $ = layui.$;
        var x = 1;
        //动态添加input输入框
        $("#add").click(function () {
            var str =
                '<div class="layui-form-item" name="last[]" id="main" tag="0">'+
                '<div class="layui-form-item">' +
                '<label class="layui-form-label">主号</label>' +
                '<div class="layui-input-inline">' +
                '<input type="text" name="mainNumber[]"  id="mainNumber_' + x + '" required  lay-verify="required" placeholder="请输入账号" autocomplete="off" class="layui-input">' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">每单注数</label>' +
                '<div class="layui-input-inline">' +
                '<input type="tel" name="note[]" id="note_' + x + '" placeholder="请输入" autocomplete="off" class="layui-input">' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">下单金额</label>' +
                '<div class="layui-input-inline">' +
                '<input type="tel" name="money[]" id="money_' + x + '" placeholder="请输入" autocomplete="off" class="layui-input">' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">止盈设置</label>' +
                '<div class="layui-input-inline">' +
                '<input type="tel" name="take[]" id="take_' + x + '" placeholder="请输入" autocomplete="off" class="layui-input">' +
                '</div>' +
                '</div>' +
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label">下单间隔</label>' +
                '<div class="layui-input-inline">' +
                '<input type="tel" name="interval_one[]" id="interval_one_' + x + '"  placeholder="请输入" autocomplete="off" class="layui-input">' +
                '</div>' +
                '</div>' +
                '<div class="layui-inline">' +
                '<div class="layui-input-inline">' +
                '<input type="tel" name="interval_one[]" id="interval_two_' + x + '"  placeholder="请输入" autocomplete="off" class="layui-input">' +
                '</div>' +
                '    <div class="layui-form-item">\n' +
                '        <div class="layui-input-inline" id="difference_'+x+'">\n' +
                '\n' +
                '        </div>\n' +
                '        <div class="layui-input-inline" id="interval_one_load_'+x+'">\n' +
                '\n' +
                '        </div>\n' +
                '        <div class="layui-input-inline" id="Interval_two_load_'+x+'">\n' +
                '\n' +
                '        </div>\n' +
                '        <div class="layui-input-inline" id="balance_'+x+'">\n' +
                '\n' +
                '        </div>\n' +
                '        <div class="layui-input-inline" id="value_'+x+'">\n' +
                '\n' +
                '        </div>\n' +
                '    </div>'+
                '</div>' +
                '<div class="layui-form-item">' +
                '<div class="layui-inline">' +
                '<label class="layui-form-label"></label>' +
                '<button name="subOrder[]" type="button" onclick="subOrder(' + x + ')"  class="layui-btn layui-btn-normal layui-btn-sm">' +
                '<i class="layui-icon">开始下单</i>' +
                '</button>' +
                '<button name="stopOrder[]" type="button" onclick="stopOrder(' + x + ')" class="layui-btn layui-btn-normal layui-btn-sm">' +
                '<i class="layui-icon">停止下单</i>' +
                '</button>' +
                '</div>' +
                '</div>' +
                '<div class="layui-input-block" >' +
                '<button type="button" class="layui-btn layui-btn-danger layui-btn-sm removeclass"><i class="layui-icon">移除账号</i></button>' +
                '</div>' +
                '</div>';

            $("#main").append(str);
            x++;
        });
        //删除动态添加的input输入框
        $("body").on('click', ".removeclass", function () {
            //元素移除前校验是否被引用
            var approvalName = $(this).parent().prev('div.layui-input-inline').children().val();
            var parentEle = $(this).parent().parent();
            parentEle.remove();
        });
        //登录
        $("#login").click(function () {
            var subdata = [];
            $("div[name='last[]']").each(function (index, element) {
                var data = {
                    "mainNumber": $(element).find("input[name='mainNumber[]']").val(),
                    "note": $(element).find("input[name='note[]']").val(),
                    "money": $(element).find("input[name='money[]']").val(),
                    "take": $(element).find("input[name='take[]']").val(),
                    "interval_one": $(element).find("input[name='interval_one[]']").val(),
                    "interval_one": $(element).find("input[name='interval_two[]']").val()
                };
                subdata.push(data);
            });
            // console.log(JSON.stringify(subdata));
            $.ajax({
                url: "./login",
                type: "post",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify({"masterList": subdata}),
                dataType: "json",
                success: function (data) {
                    if(data){
                        alert("登录成功")
                    }else{
                        alert("登录失败")
                    }

                }
            })
        })
    });

    $(function () {
        var interval = setInterval(function(){
            $("div[name='last[]']").each(function (index, element) {
               var mainNumber = $(element).find("input[name='mainNumber[]']").val();
                if(mainNumber!=""&&mainNumber!="undefiend"){
                    $.ajax({
                        "url": "./loadOrderNum",
                        "data": {
                            "sid": mainNumber
                        },
                        "dataType": "json",
                        success: function (data) {
                            if(data!=0){
                               $(element).find("#difference_"+index).html("单号差距："+data.difference);
                               $(element).find("#interval_one_load_"+index).html("下单间隔单号1："+data.interval_one_load);
                               $(element).find("#Interval_two_load_"+index).html("下单间隔单号2："+data.Interval_two_load);
                               $(element).find("#balance_"+index).html("余额："+data.balance);
                               $(element).find("#value_"+index).html("收益："+data.value);
                            }

                        }
                    })
                }
            });
        },1000)
    });

    //开始下单
    function subOrder(num) {
        $.ajax({
            "url": "./startOrStop",
            "data": {
                "sid": $("#mainNumber_" + num).val(),
                "money": $("#money_" + num).val(),
                "interval_one": $("#interval_one_" + num).val(),
                "interval_two": $("#interval_two_" + num).val(),
                "flag": 1
            },
            "dataType": "json",
            success: function (data) {
                alert("开始下单");
            }
        })
    }

    //停止下单
    function stopOrder(num) {
        $.ajax({
            "url": "./startOrStop",
            "data": {
                "sid": $("#mainNumber_" + num).val(),
                "money": $("#money_" + num).val(),
                "interval_one": $("#interval_one_" + num).val(),
                "interval_two": $("#interval_two_" + num).val(),
                "flag": 0
            },
            "dataType": "json",
            success: function (data) {
                alert("停止下单");
            }
        })
    }


</script>


</html>